//
//  AccountDepositRequestViewController.m
//  HYApp
//
//  Created by Jianbo Cai on 2020/8/11.
//  Copyright © 2020 Unidax. All rights reserved.
//

#import "AccountDepositRequestViewController.h"
#import <JFKit/FZAlertController.h>
#import "HYNetHelper.h"
#import "ChangeVerifyView.h"
#import "IncomingDetailsViewController.h"

@implementation DepositRequestEntity

@end


@interface AccountDepositRequestViewController ()

@property (weak, nonatomic) IBOutlet UILabel *depositLabel;
@property (weak, nonatomic) IBOutlet UILabel *countryLabel;
@property (weak, nonatomic) IBOutlet UILabel *methodLabel;

@property (weak, nonatomic) IBOutlet UILabel *messageTitle;
@property (weak, nonatomic) IBOutlet UILabel *label1;
@property (weak, nonatomic) IBOutlet UILabel *label2;
@property (weak, nonatomic) IBOutlet UILabel *label3;

@property (weak, nonatomic) IBOutlet UIButton *ConfirmButton;


@end

@implementation AccountDepositRequestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.messageTitle.text = JFMainLocalizedString(@"withdraw.amount-not-available-after-request");
    self.label2.text = JFMainLocalizedString(@"withdraw.country");
    if (self.type == 0) { // Deposit
        self.messageTitle.text = nil;
        self.title = JFMainLocalizedString(@"deposit.confirm-deposit");
        self.label3.text = JFMainLocalizedString(@"deposit.method");
        self.label1.text = JFMainLocalizedString(@"deposit.deposit-confirm");
    }else if (self.type == 3){
        self.title = JFMainLocalizedString(@"withdraw.confirm-withdraw");
        self.label3.text = JFMainLocalizedString(@"withdraw.method");
        self.label1.text = JFMainLocalizedString(@"withdraw.receive-amount");
    }
    [self.ConfirmButton setTitle:JFMainLocalizedString(@"confirm") forState:UIControlStateNormal];
    
    self.depositLabel.text = self.entity.amount;
    self.countryLabel.text = self.entity.country;
    self.methodLabel.text = self.entity.method;
}

- (void)requestDeposit {
    WEAKSELF
    [HYNetHelper deposit:self.entity.amount wallet_id:self.entity.wallet_id method_id:self.entity.method_id country_iso3166:self.entity.countryId currency_id:self.entity.currency_id user_bank_account_id:self.entity.bankAccount.ID callBack:^(JFResultVo *resultVo) {
        if (!resultVo.errorNo) {
//            NSString *message = [NSString stringWithFormat:@"You have requested to deposit %@. We will process your request and send you instructions for next step(s) via email.", weakSelf.entity.asset_symbol];
            NSString *transfer_id = resultVo.results[@"transfer_id"];
            
            NSString *message = [NSString stringWithFormat:JFMainLocalizedString(@"deposit.notification-success-description"), weakSelf.entity.amount, weakSelf.entity.asset_symbol];
            FZAlertController *alertController = [FZAlertController alertControllerWithTitleImage:[UIImage imageNamed:@"alert_icon1"] title:JFMainLocalizedString(@"deposit.deposit-success") customView:nil message:message];
            alertController.showCloseBtn = YES;
            alertController.messageAlignment = NSTextAlignmentCenter;
            
            FZAlertAction *action = [FZAlertAction actionWithTitle:JFMainLocalizedString(@"deposit.see-details") style:UIAlertActionStyleDefault handler:^(FZAlertAction *action) {
                [weakSelf pushIncomingDetailsVC:transfer_id];
                                }];
            [alertController addAction:action];
            [self presentViewController:alertController animated:YES completion:nil];
        }else {
            FZAlertController *alertController = [FZAlertController alertControllerWithTitleImage:[UIImage imageNamed:@"alert_icon_failed"] title:@"Deposit failed" customView:nil message:nil];
            alertController.showCloseBtn = YES;
            alertController.messageAlignment = NSTextAlignmentCenter;
            
            FZAlertAction *action = [FZAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(FZAlertAction *action) {
                                }];
            [alertController addAction:action];
            [self presentViewController:alertController animated:YES completion:nil];
        }
    }];
}

- (void)requestWithdrawToken:(NSString *)token {
    WEAKSELF
    [HYNetHelper withdarwFiat:self.entity.amount wallet_id:self.entity.wallet_id withdraw_method_id:self.entity.method_id currency_id:self.entity.currency_id data:self.entity.withdrawBankData token:token callBack:^(JFResultVo *resultVo) {
        if (!resultVo.errorNo) {
//            NSString *message = [NSString stringWithFormat:@"You have requested for withdrawal of %@. We will process your request and send you instructions for next step(s) via email.", weakSelf.entity.asset_symbol];
            NSString *transfer_id = resultVo.results[@"transfer_id"];
            
            NSString *message = [NSString stringWithFormat:JFMainLocalizedString(@"withdraw.notification-success-description"), weakSelf.entity.amount, weakSelf.entity.asset_symbol];
            FZAlertController *alertController = [FZAlertController alertControllerWithTitleImage:[UIImage imageNamed:@"alert_icon1"] title:JFMainLocalizedString(@"deposit.deposit-success") customView:nil message:message];
            alertController.showCloseBtn = YES;
            alertController.messageAlignment = NSTextAlignmentCenter;
            
            FZAlertAction *action = [FZAlertAction actionWithTitle:JFMainLocalizedString(@"deposit.see-details") style:UIAlertActionStyleDefault handler:^(FZAlertAction *action) {
                [weakSelf pushIncomingDetailsVC:transfer_id];
                                }];
            [alertController addAction:action];
            [self presentViewController:alertController animated:YES completion:nil];
        }else {
            FZAlertController *alertController = [FZAlertController alertControllerWithTitleImage:[UIImage imageNamed:@"alert_icon_failed"] title:@"Withdraw Requested" customView:nil message:@"Your requested is faild."];
            alertController.showCloseBtn = YES;
            alertController.messageAlignment = NSTextAlignmentCenter;
            
            FZAlertAction *action = [FZAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(FZAlertAction *action) {
                                }];
            [alertController addAction:action];
            [self presentViewController:alertController animated:YES completion:nil];
        }
    }];
}

- (IBAction)confirmAction:(id)sender {
    if (self.type == 3) {
        ChangeVerifyView *view = [[[NSBundle mainBundle] loadNibNamed:@"ChangeVerifyView" owner:nil options:nil] firstObject];
        view.frame = CGRectMake(0, 0, CUSTOMVIEW_WIDTH, 110);
        FZAlertController *vc = [FZAlertController alertControllerWithTitleImage:[UIImage imageNamed:@"Captcha_ico_b"] title:nil customView:view message:nil];
        vc.showCloseBtn = YES;
        WEAKSELF
        FZAlertAction *action = [FZAlertAction actionWithTitle:JFMainLocalizedString(@"confirm") style:UIAlertActionStyleDefault handler:^(FZAlertAction *action) {
            [weakSelf requestWithdrawToken:view.codeText.text];
                            }];
        [vc addAction:action];
        [self presentViewController:vc animated:YES completion:nil];
    }else {
        [self requestDeposit];
    }
}

- (void)pushIncomingDetailsVC:(NSString *)transfer_id {
    IncomingDetailsViewController *vc = [[UIStoryboard storyboardWithName:@"User" bundle:nil]instantiateViewControllerWithIdentifier:@"IncomingDetailsViewController"];
    vc.transfer_id = transfer_id;
    [self.navigationController pushViewController:vc animated:YES];
}

@end
